package freespiritsocial.io.user.ui.fragment.account

import android.graphics.Paint
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import freespiritsocial.io.user.R
import freespiritsocial.io.user.base.BaseFragment
import freespiritsocial.io.user.databinding.FrgVerificationCodeBinding
import freespiritsocial.io.user.ext.setTitle
import freespiritsocial.io.user.handle.BackHandle
import freespiritsocial.io.user.ui.activity.MainAct
import freespiritsocial.io.user.util.CountDownTimerHelper
import freespiritsocial.io.user.viewmodel.SignUpModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

class VerificationCodeFrg: BaseFragment<SignUpModel,FrgVerificationCodeBinding>() {
    private val timerHelper by lazy(LazyThreadSafetyMode.NONE) {
        CountDownTimerHelper(lifecycleScope,mViewModel.countDownTip)
    }

    override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
        //设置view model作用域
        viewModelOwner = (activity as MainAct).accountViewModelOwner
        super.onViewCreated(view,savedInstanceState)
        view.setTitle(getString(R.string.code))
    }

    override fun layoutId(): Int = R.layout.frg_verification_code

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewModel = mViewModel
        mDatabind.click = Click()
        mDatabind.tvCountdownTip.paintFlags = Paint.UNDERLINE_TEXT_FLAG
    }

    override fun lazyLoadData() {
        mViewModel.sendSmsCode()
        timerHelper.startCountDown()
    }

    override fun onDestroyView() {
        mViewModel.code.set("")
        timerHelper.cancel()
        super.onDestroyView()
    }


    inner class Click: VerificationCodeClick {
        override fun sendCode() {
            mViewModel.sendSmsCode()
            timerHelper.startCountDown()
        }

        override fun submit() {
            mViewModel.checkSmsCode {
                nav().navigateAction(R.id.action_verificationCodeFrg_to_signSup2UpFrg)
            }
        }

        override fun goBack() {
            nav().navigateUp()
        }
    }
}